package com.orange.core.config.convert;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class StringStripModule extends SimpleModule {

    public StringStripModule() {
        addDeserializer(String.class, new StdScalarDeserializer<>(String.class) {
            @Override
            public String deserialize(JsonParser parser, DeserializationContext deserializationContext) throws IOException {
                String value = parser.getValueAsString();
                if (StrUtil.isNotBlank(value)) {
                    return value.strip();
                }
                return value;
            }
        });
    }
}
